<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of debug
 *
 * @author Nazmul Hossain <nazmul.eps@gmail.com>
 */
class debug {

    private $ext;
    private $handle;
    private $success;
    private $filename;
    private $folderpath;

    #Class constructor

    public function __construct($_folderpath = "debug", $_ext = "txt", $_filename = "", $delete = false) {
        $this->filename = $_filename;
        $this->extension($_ext);
        $this->folderpath($_SERVER['DOCUMENT_ROOT'] . "/googledata/" . $_folderpath);
        if (!empty($_folderpath) && !empty($_ext) && $delete === false) {
            if (file_exists($this->folderpath)) {
                if (empty($_filename)) {
                    $this->filename = "debug-" . $this->date();
                }
                if (!$this->fileExists()) {
                    $this->createFile();
                }
            }
        } else if ($delete === true) {
            if (empty($_filename)) {
                exit("File Name Missing");
            } else if ($this->fileExists($this)) {
                exit("File Not Found");
            } else {
                $success = $this->fileDelete($this);
                exit("Delete Successfully :: " . $this->filename . $this->ext);
            }
        }
    }

    public function setAddMessage($message, $hint = "") {
        $this->appendFile($message, $hint);
    }

    private function appendFile($message, $hint) {
        try {
            $this->success = FALSE;
            $_hint = empty($hint) ? "" : "$hint ";
            $datetime = "\n[$_hint" . $this->time() . "] :: \n";
            $file = $this->folderpath . $this->filename . $this->ext;
            $this->handle = fopen($file, 'a');
            fwrite($this->handle, $datetime . $message);
            $this->fileClose();
            $this->success = TRUE;
        } catch (Exception $exception) {
            $this->success = FALSE;
            debug($exception);
        }
        return $this->success;
    }

    private function createFile() {
        try {
            $file = $this->folderpath . $this->filename . $this->ext;
            $this->handle = fopen($file, 'w');
            $data = 'Debug ' . $this->datetime();
            fwrite($this->handle, $data);
            if ($this->handle)
                $this->success = TRUE;
            $this->fileClose();
        } catch (Exception $exception) {
            $this->success = FALSE;
            debug($exception);
        }
        return $this->success;
    }

    private function fileClose() {
        try {
            fclose($this->handle);
            $this->success = TRUE;
        } catch (Exception $exception) {
            $this->success = FALSE;
            debug($exception);
        }
        return $this->success;
    }

    private function fileDelete() {
        try {
            $file = $this->folderpath . $this->filename . $this->ext;
            unlink($file);
            $this->success = TRUE;
        } catch (Exception $exception) {
            $this->success = FALSE;
            debug($exception);
        }
        return $this->success;
    }

    private function fileExists() {
        try {
            $file = $this->folderpath . $this->filename . $this->ext;
            $this->success = file_exists($file);
        } catch (Exception $exception) {
            $this->success = FALSE;
            debug($exception);
        }
        return $this->success;
    }

    private function extension($ext) {
        try {
            $_ext = substr($ext, 0, 1);
            if ($_ext === ".") {
                $this->ext = $ext;
            } else {
                $this->ext = "." . $ext;
            }
        } catch (Exception $exception) {
            debug($exception);
        }
        return;
    }

    private function folderpath($folderpath) {
        try {
            $_folderpath = substr($folderpath, (strlen($folderpath) - 1), strlen($folderpath));
            if ($_folderpath === "/") {
                $this->folderpath = $folderpath;
            } else {
                $this->folderpath = $folderpath . "/";
            }
        } catch (Exception $exception) {
            debug($exception);
        }
        return;
    }

    private function datetime() {
        try {
            $info = getdate();
            $date = $info['mday'];
            $month = $info['mon'];
            $year = $info['year'];
            $hour = $info['hours'];
            $min = $info['minutes'];
            $sec = $info['seconds'];

            $current_date_time = "$date-$month-$year $hour:$min:$sec";
        } catch (Exception $exception) {
            debug($exception);
        }
        return $current_date_time;
    }

    private function time() {
        try {
            $info = getdate();
            $hour = $info['hours'];
            $min = $info['minutes'];
            $sec = $info['seconds'];

            $current_time = "$hour:$min:$sec";
        } catch (Exception $exception) {
            debug($exception);
        }
        return $current_time;
    }

    private function date() {
        try {
            $info = getdate();
            $date = $info['mday'];
            $month = $info['mon'];
            $year = $info['year'];

            $current_date = "$date-$month-$year";
        } catch (Exception $exception) {
            debug($exception);
        }
        return $current_date;
    }

}

function wdebug($message, $hint = "") {
    $debugObject = new Debug();
    $debugObject->setAddMessage($message, $hint);
}

?>
